using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSettingViewController : ViewController, UISettingListener {


    public const int userNameMin = 2;
    public const int userNameMax = 30;

    static string UsernameKey => "Username_Key";
    public static string username
    {
        get
        {
            return PlayerPrefs.GetString(UsernameKey);
        }
        set
        {
            PlayerPrefs.SetString(UsernameKey, value);
        }
    }
    private static string PasswordKey => "Password_Key";
    public static string password
    {
        get
        {
            return PlayerPrefs.GetString(PasswordKey);
        }
        set
        {
            PlayerPrefs.SetString(PasswordKey, value);
        }
    }

    SettingPanelData m_SettingPanelData = new SettingPanelData();

    public string uiPrefabPath = ViewPath.SettingPanel;
    SettingPanel m_GameSettingPanel;

    protected override void WillLoadView()
    {
        if (!m_GameSettingPanel)
        {
            var prefab = ResourceManager.Load<SettingPanel>(uiPrefabPath);
            m_GameSettingPanel = Instantiate(prefab, transform);
            m_GameSettingPanel.SetViewController(this);
        }

        m_SettingPanelData.musicOn = Game.Instance.gameSetting.bgMusic;
        m_SettingPanelData.soundOn = Game.Instance.gameSetting.soundEffect;
        m_SettingPanelData.vibrationOn = Game.Instance.gameSetting.vibration;
        m_SettingPanelData.TestingMode = Game.Instance.gameSetting.testingMode;
        m_SettingPanelData.languageType = Localization.language;
        
        m_GameSettingPanel.Init(m_SettingPanelData);
    }

    public override void WillHideView()
    {
        base.WillHideView();
        if(m_GameSettingPanel)
            m_GameSettingPanel.PlayHideAnim();
    }

    public override void OnViewDelete()
    {
        base.OnViewDelete();

        if (m_GameSettingPanel)
            Destroy(m_GameSettingPanel.gameObject);
    }

 

    #region UISettingListener



/*    public void OnClickConfirm()
    {
        Game.Instance.gameSetting.bgMusic = m_GameSettingData.musicOn;
        Game.Instance.gameSetting.soundEffect = m_GameSettingData.soundOn;
        Game.Instance.gameSetting.testingMode = m_GameSettingData.TestingMode;
        LanguageTypeSet.language = m_GameSettingData.languageType;
    }*/

    public void OnMusicToggle(bool isOn)
    {
        GameDebug.Log($"设置音乐开关：{isOn}");
        if (m_SettingPanelData.musicOn != isOn)
        {
            m_SettingPanelData.musicOn = isOn;
            Game.Instance.gameSetting.bgMusic = m_SettingPanelData.musicOn;

            if (isOn)
            {
                AudioController.instance.StopAllMusic();
                var homeMusic = ResourceManager.Load<AudioClip>(ResourceManager.homeSceneMusic);
                AudioController.instance.Play(homeMusic, Vector3.zero, true, null, true,.5f);

                //GameDebug.LogError("OnMusicToggle on");
            }

        }
    }

    public void OnSoundToggle(bool isOn)
    {
        GameDebug.LogError($"设置音效开关：{isOn}");
        m_SettingPanelData.soundOn = isOn;
        Game.Instance.gameSetting.soundEffect = m_SettingPanelData.soundOn;
    }
    public void OnVibrationToggle(bool isOn)
    {
        GameDebug.LogError($"设置震动开关：{isOn}");
        m_SettingPanelData.vibrationOn = isOn;
        Game.Instance.gameSetting.vibration = m_SettingPanelData.vibrationOn;
    }

    public void OnLanguageType(int type)
    {
        if ((LanguageType)type != m_SettingPanelData.languageType)
        {
            ShowRestartNotice((LanguageType) type);
        }
    }

    public void OnTestMode(bool isOn)
    {
        m_SettingPanelData.TestingMode = isOn;
        Game.Instance.gameSetting.testingMode = m_SettingPanelData.TestingMode;
    }
    
    public void ShowRestartNotice(LanguageType toSetLangType)
    {
        string title = Localization.instance.GetValue("news");
        string content = Localization.instance.GetValue("setting_panel_restart_content");
        string confirmTxt = Localization.instance.GetValue("confirm");
        // this.parentNav.LoadDisplay<NoticePanelDisplayController>((p) => {
        Game.Instance.noticePanel.Init(new NoticePanelData(title, content, confirmTxt,  Localization.instance.GetValue("cancel"),()=>
        {
            Localization.language = toSetLangType;
            Application.Quit();
        }, () =>
        {
            m_GameSettingPanel.Init(m_SettingPanelData);

        }));
        // },false);
    }

    #endregion

}
